package com.wanma.recuit.webcontroller;


import com.wanma.recuit.util.TimestampEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;

import java.util.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

@ControllerAdvice //控制器建议  一般统一的异常处理 日期转换 对所有的控制器都起作用
//如果某个控制器中定义了异常处理和日期转换，则按着控制器的定义来，如果没有则按着控制器的建议
public class CommonControllerAdvice {

    @ExceptionHandler(Exception.class)
    public String processException(Exception e)
    {
        return "service/error";
    }

    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
        binder.registerCustomEditor(Timestamp.class,new TimestampEditor("yyyy-MM-dd"));
    }


}
